package com.itheima.springboot02_01condition.config;

import com.itheima.springboot02_01condition.pojo.Apple;
import com.itheima.springboot02_01condition.pojo.User;
import org.springframework.beans.factory.support.AbstractBeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;
import org.springframework.core.type.AnnotationMetadata;


public class MyImportBeanDefinitionRegistrar implements ImportBeanDefinitionRegistrar{

    /**
     * @param importingClassMetadata 元数据(不用)
     * @param registry  bean 定义对象的注册器
     */
    @Override
    public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {

        //创建beanDefinition对象（bean 定义对象）
        AbstractBeanDefinition beanDefinition1 = BeanDefinitionBuilder.rootBeanDefinition(User.class).getBeanDefinition();
        AbstractBeanDefinition beanDefinition2 = BeanDefinitionBuilder.rootBeanDefinition(Apple.class).getBeanDefinition();

        //把 bean定义对象注册到容器中(key,value)
        registry.registerBeanDefinition("user" , beanDefinition1);
        registry.registerBeanDefinition("apple" , beanDefinition2);

    }
}
